package com.qiyeji.real_estate.controller;

import com.qiyeji.real_estate.entity.customer;

import com.qiyeji.real_estate.service.CustomerService;

import com.qiyeji.real_estate.utils.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/Customer")
@RestController
@ResponseBody
public class CustomerController {
    @Autowired
    CustomerService customerService;

    @PostMapping("/getAllCustomer")
    public Result getAllCustomer() {
        List<customer> customers = customerService.getAllCustomer();
        if(customers==null) {
            return Result.error();
        }
        else {
            return Result.success(customers);
        }
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody Map<String, String> params){
        int res=customerService.insert(new customer(
                params.get("cID"),
                params.get("cName"),
                params.get("tel"),
                params.get("qq"),
                params.get("email"),
                params.get("primaryUnit"),
                params.get("secondaryUnit"),
                params.get("tertiaryUnit")
        ));
        if(res==0) {
            return Result.error();
        }
        else {
            return Result.success(res);
        }
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Map<String,String> params){
        String cID = params.get("cid");
        int res=customerService.delete(cID);
        if(res==0){
            return Result.error();
        }else{
            return Result.success(res);
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody Map<String, String> params){
        int res=customerService.update(new customer(
                params.get("cid"),
                params.get("cname"),
                params.get("tel"),
                params.get("qq"),
                params.get("email"),
                params.get("primaryunit"),
                params.get("secondaryunit"),
                params.get("tertiaryunit"))
        );
        if(res==0){
            return Result.error();
        }else{
            return Result.success(res);
        }
    }

    @PostMapping("/selectById")
    public Result selectById(@RequestBody Map<String,String> params){
        String cID = params.get("cID");
        customer customer=customerService.selectById(cID);
        if(customer==null){
            return Result.error();
        }else{
            return Result.success(customer);
        }
    }
    @PostMapping("/getCustomerByProjectId")
    public Result getCustomerByProjectId(@RequestBody Map<String,String> params){
        String pCode = params.get("pCode");
        customer customer=customerService.getCustomerByProjectId(pCode);
        if(customer==null){
            return Result.error();
        }else{
            return Result.success(customer);
        }
    }

    @PostMapping("/fuzzyQuery")
    public Result fuzzyQuery(@RequestBody Map<String,String> params){
        String key = params.get("key");
        List<customer> customers = customerService.fuzzyQuery(key);
        if(customers==null) {
            return Result.error();
        }
        else {
            return Result.success(customers);
        }
    }

    @PostMapping("/getCustomerByProjectName")
    public Result getCustomerByProjectName(@RequestBody Map<String,String> params){
        String pName = params.get("pName");
        customer customer=customerService.getCustomerByProjectName(pName);
        if(customer==null){
            return Result.error();
        }else{
            return Result.success(customer);
        }
    }
}
